/* File: valeant_plots.do
 * Author: Luca Maini
 * Purpose: this file creates additional graphs for Valeant acquisitions
 *
 */

graph set window fontface "${paperFont}"

*** Plots of Valeant raw data
use "${maindir}\combined_regression_dataset_with_valeant_augmented.dta", clear

* select only products acquired by Valeant
bysort Product : egen acquired_by_valeant = max(valeant_acqd)
keep if acquired_by_valeant == 1
drop acquired_by_valeant

* create time-from-acquisition variable
bysort Product (year) : egen acq_year = max(year * valeant_acqd)
gen y_from_acq = year - acq_year

* create samples based on how many years of data each drug has (around the 
* acquisition)
forvalues i = -3/3 {
	local j = `i' + 3
	bysort Product : egen has`j' = max(y_from_acq == `i' & net_sales != .)
	}
	
gen sample3 = has0 == 1 & has1 == 1 & has2 == 1 & has3 == 1 & has4 == 1 & has5 == 1 & has6 == 1
gen sample2 = has1 == 1 & has2 == 1 & has3 == 1 & has4 == 1 & has5 == 1
gen sample1 = has2 == 1 & has3 == 1 & has4 == 1


gen y_from_acqREG = y_from_acq + 4	// must have positive values to create indicators

local yList "log_sales log_units"

foreach var of local yList {
	areg `var' b3i.y_from_acqREG age ///
		if y_from_acq >= -3 & y_from_acq <= 3 & sample1 == 1, ///
			absorb(Product)
	
	forvalues i = 1/7 {
		local b`i'_`var' = _b[`i'.y_from_acqREG]
		local se`i'_`var' = _se[`i'.y_from_acqREG]
		}
	}


clear

set obs 7

egen y_from_acq = seq()
replace y_from_acq = y_from_acq - 4.025

gen log_sales = .
gen log_sales_UB = .
gen log_sales_LB = .
	
forvalues i = 1/7 {
	replace log_sales = `b`i'_log_sales' in `i'
	replace log_sales_UB = `b`i'_log_sales' + 1.96 * `se`i'_log_sales' in `i'
	replace log_sales_LB = `b`i'_log_sales' - 1.96 * `se`i'_log_sales' in `i'
	}

tempfile log_sales
save `log_sales', replace

clear
set obs 7

egen y_from_acq = seq()
replace y_from_acq = y_from_acq - 3.975

gen log_units = .
gen log_units_UB = .
gen log_units_LB = .
	
forvalues i = 1/7 {
	replace log_units = `b`i'_log_units' in `i'
	replace log_units_UB = `b`i'_log_units' + 1.96 * `se`i'_log_units' in `i'
	replace log_units_LB = `b`i'_log_units' - 1.96 * `se`i'_log_units' in `i'
	}

append using `log_sales'

*** Figure A5
twoway 	(connect log_sales log_units y_from_acq, ///
			color(gs0 gs0) msymbol(O S) lpattern(solid dash)) ///
		(rspike log_sales_UB log_sales_LB y_from_acq, ///
			lcolor(gs0)) ///
		(rspike log_units_UB log_units_LB y_from_acq, ///
			lcolor(gs0)) ///
	, ///
		xlabel(-3(1)3, labsize(small)) ///
		ylabel(#5, labsize(small) angle(0)) ///
		xtitle("Years from acquisition", height(5)) ytitle("Log change in net sales") ///
		legend(order(1 "Net sales" 2 "Volume") ring(0) bplacement(11) region(color(none))) ///
		yline(0, lcolor(gs0)) ///
		xline(0, lwidth(*15) lcolor(gs10%50))
graph export "${paperdir}\Figures\Figure A2.pdf", as(pdf) replace
graph export "${paperdir}\Figures\Figure A2.eps", as(eps) replace
